//
//  AlipayPageTestTabBarViewController.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/5/6.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "AlipayPageTabBarViewController.h"
#import "APNavigationViewController.h"
#import "APHomeViewController.h"
#import "APLCViewController.h"
#import "APViewModel.h"

@interface AlipayPageTabBarViewController()<UIGestureRecognizerDelegate>

proStrong UIView *contactView;
proStrong UIControl *contactControl;

@end

@implementation AlipayPageTabBarViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    
    self.tabBar.tintColor = [APViewModel themeColor];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setUpVCs];
    [self setUpFloatBtn];
}

- (void)setUpVCs {
    
    APHomeViewController *homeVC = [[APHomeViewController alloc] init];
    APNavigationViewController *homeNav = [[APNavigationViewController alloc] initWithRootViewController:homeVC];
    UITabBarItem *homeTabItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"tab_home"] tag:0];
    homeVC.tabBarItem = homeTabItem;
    
    APLCViewController *lcVC = [[APLCViewController alloc] init];
    APNavigationViewController *lcNav = [[APNavigationViewController alloc] initWithRootViewController:lcVC];
    UITabBarItem *lcTabItem = [[UITabBarItem alloc] initWithTitle:@"理财" image:[UIImage imageNamed:@"tab_query"] tag:1];
    lcVC.tabBarItem = lcTabItem;
    
    self.viewControllers = @[homeNav,lcNav];
}

- (void)setUpFloatBtn {
    
    self.contactView = [[UIView alloc] initWithFrame:CGRectMake(self.view.width-86, self.view.height-90-kTabBarBarHeight, 86, 82)];
    [self.view addSubview:self.contactView];
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 66, 62)];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.textColor = [UIColor whiteColor];
    lable.text = @"退出";
    [self.contactView addSubview:lable];
    [Utils resetView:lable bgColor:[APViewModel themeColor] cornerRadius:10 borderWidth:0 borderColor:nil];
    self.contactControl = [[UIControl alloc] initWithFrame:lable.frame];
    [self.contactControl addTarget:self action:@selector(popOutAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contactView addSubview:self.contactControl];
    
    UIPanGestureRecognizer* singleTwoTap = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(touchesTwoMoved:)];
    singleTwoTap.delegate = self;
    singleTwoTap.cancelsTouchesInView = YES;
    [self.contactView addGestureRecognizer:singleTwoTap];
}

- (void)popOutAction {
    
    [Utils showAlertWithTitle:nil content:@"退出？" targetVC:self dismissBlock:^(NSInteger index) {
        if (index == 0) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    } cancelButton:@"确定" otherButtons:@[@"取消"]];
}

#pragma mark -
#pragma mark contact gesture

- (void)touchesTwoMoved:(UIPanGestureRecognizer *)touches {
    
    self.contactControl.enabled = NO;
    
    UIView *view = touches.view;
    if (touches.state == UIGestureRecognizerStateBegan ||
        touches.state == UIGestureRecognizerStateChanged) {
        
        CGPoint translation = [touches translationInView:view.superview];
        [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
        [touches setTranslation:CGPointZero inView:view.superview];
    }
    
    if (touches.state == UIGestureRecognizerStateEnded) {
        
        CGFloat left = view.superview.width-view.width;
        CGFloat top = view.top;
        if (top < 0) {
            top = 0;
        } else if (view.top > view.superview.height-view.height-10) {
            top = view.superview.height-view.height-10;
        }
        [UIView animateWithDuration:0.25 animations:^{
            view.frame = CGRectMake(left, top, view.width, view.height);
        }];
        
        self.contactControl.enabled = YES;
    }
}


@end
